//
//  JourneyHeaderViewController.m
//  Travel_M_Travel
//
//  Created by APPLE10 on 15-3-24.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "JourneyHeaderViewController.h"
#import "JourneryNetHandler.h"
#import "JourneyHeaderDataSource.h"
#import "JourneyHeaderDetilCollectionViewCell.h"
@interface JourneyHeaderViewController ()<UICollectionViewDelegate , UICollectionViewDataSource>
@property (nonatomic , retain)UICollectionView *detilCLV;
@property (nonatomic , retain)NSMutableArray *headerArr;
@end

@implementation JourneyHeaderViewController

-(void)dealloc
{
    [_detilCLV release];
    
    [super dealloc];
    
}

- (void) handlerNet
{
    NSString *str = [NSString stringWithFormat:@"http://api.breadtrip.com/destination/index_places/%ld/", self.index];
    NSLog(@"%@",str);
    
    [JourneryNetHandler getDataWithUrl:str completion:^(NSData *data) {
        NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSMutableArray *resultArr =[resultDic objectForKey:@"data"];
        self.headerArr = [NSMutableArray array];
        for (NSMutableDictionary *dic in resultArr) {
            JourneyHeaderDataSource *headerData = [[JourneyHeaderDataSource alloc]initWithDictionary:dic];
            [self.headerArr addObject:headerData];
            [headerData release];
        }
        
        [_detilCLV reloadData];
        NSLog(@"%@",_headerArr);
    }];
  
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self handlerNet];
    
    // Do any additional setup after loading the view.
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(130, 150);
    //最小列间距
    flowLayout.minimumInteritemSpacing = 1;
    //最小行间距
    flowLayout.minimumLineSpacing = 10;
    //边缘距离
    flowLayout.sectionInset = UIEdgeInsetsMake(20, 10, 20, 15);
    
    self.detilCLV = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
    _detilCLV.backgroundColor = [UIColor cyanColor];
    _detilCLV.delegate = self;
    _detilCLV.dataSource = self;
    [self.view addSubview:_detilCLV];
    
    [_detilCLV registerClass:[JourneyHeaderDetilCollectionViewCell class] forCellWithReuseIdentifier:@"详情页面"];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [_headerArr count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    JourneyHeaderDetilCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"详情页面" forIndexPath:indexPath];
    cell.headerData = [_headerArr objectAtIndex:indexPath.item];
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
